package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.DtProduct;
import com.ruoyi.project.system.domain.vo.QueryItem;
import com.ruoyi.project.system.mapper.DtFinalResultMapper;
import com.ruoyi.project.system.mapper.DtProductMapper;
import com.ruoyi.project.system.service.IDtProductService;
import com.ruoyi.project.system.service.IDtRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询产品Controller
 */
@RestController
@RequestMapping("/system/product")
public class DtProductController extends BaseController {

    @Autowired
    private IDtRiskService dtRiskService;

    @Autowired
    private IDtProductService dtProductService;

    @Autowired
    private DtProductMapper dtProductMapper;

    @Autowired
    private DtFinalResultMapper dtFinalResultMapper;

    /**
     * 查询
     */
    @GetMapping("/recordOption")
    public AjaxResult recordOption() {
        Long userId = SecurityUtils.getUserId();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("item", dtProductMapper.queryEnableProductList(userId, "query"));
        ajaxResult.put("riskLabel", dtRiskService.riskLabel(null));
        return ajaxResult;
    }

    @GetMapping("/userOption")
    public AjaxResult userOption() {
        return success(dtProductMapper.userChoice());
    }

    @GetMapping("/choiceItem")
    public AjaxResult choiceItem() {
        return dtProductService.choiceItem();
    }

    @GetMapping("/queryItem")
    public AjaxResult queryItem(@NotBlank String type) {
        Long userId = SecurityUtils.getUserId();
        List<QueryItem> queryItems = dtProductMapper.queryEnableProductList(userId, type);
        List<String> maximumUsage = new ArrayList();
        int i = 0;
        for (QueryItem qi : queryItems) {
            if ("0".equals(qi.getStatus())) {
                qi.setExplain(qi.getMsg());
            }
            if (i++ < 3) {
                maximumUsage.add(qi.getCode());
            }
        }
        Map data = new HashMap<String, Object>();
        data.put("item", queryItems);
        data.put("maximumUsage", dtFinalResultMapper.maximumUsage(userId));
        return success(data);
    }

    /**
     * 查询查询产品列表
     */
//    @PreAuthorize("@ss.hasPermi('system:product:list')")
//    @GetMapping("/list")
//    public AjaxResult list() {
//        List<DtProduct> list = dtProductService.selectDtProductList();
//        return success(list);
//    }

    /**
     * 获取查询产品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:product:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Integer id) {
//        return success(dtProductService.selectDtProductById(id));
//    }

}
